/* Module: mouse.c
 * Author: Konstantin Wurster
 */
#include "stdafx.h"

/* returns a handle to the mouse, it must be closed with closeMouseHandle. */
MOUSE openMouseHandle()
{
	MOUSE mouse;
	SetLastError(0);
	mouse = CreateFile( MOUSE_HID, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL );
	fwprintf(stderr, TEXT("opened %s\nhandle %d\nerrorcode %x"), MOUSE_HID, mouse, GetLastError());
	return mouse;
}

/* closes the mouse handle. */
void closeMouseHandle(MOUSE mouse)
{
	CloseHandle(mouse);
}

/* selects and applies a profile. */
void setMouseProfile(MOUSE mouse, BYTE index)
{
	OVERLAPPED overlapped;
	MOUSE_DATA data;
	DWORD written;

	memset(&data, 0, sizeof(MOUSE_DATA));
	memset(&overlapped, 0, sizeof(OVERLAPPED));

	data.magic = MOUSE_MAGIC;
	data.cmd = MOUSE_CMD_SETPROFILE;
	data.d.changeprofile.unknown = MOUSE_SETPROFILE_UNK;
	data.d.changeprofile.profile = index;

	WriteFile(mouse, &data, sizeof(MOUSE_DATA), &written, &overlapped);
}